/**
 * 班组类目设置
 * 宁建浩
 * 2017/8/21  14:00
 * */

var testConfig = require('../testConfig');
const ipcRenderer = require('electron').ipcRenderer;
var l = parseInt(window.sessionStorage.getItem("runTimes"));
var planData = JSON.parse(window.sessionStorage.getItem('planData'));
var caseData =  planData.cases[l];
var msg = {
  reportId:planData.reportId,
  taskId:planData.taskId,
  caseId:caseData.caseId,
  caseNumber:caseData.caseNumber,
  isOver:'false'
};
function SearchProject() {
  this.beforeTr = 0;
  this.init = function () {
    var arr = [];
    arr.push(this.step1,
      this.step2,
      this.step3,
      this.step4);
    testConfig.testFun.runStep(arr);
  };
  this.step1 = function(){
    $("#aside-nav").find("div.in").find("li").eq(1)[0].click();
    msg.stepResult = {
      byname:"skipTeamCategoryManage",
      realData:$("#main-container iframe").attr('src')
    };
    ipcRenderer.sendToHost(JSON.stringify(msg));
  };
  this.step2 = function () {
    this.beforeTr = testConfig.testFun.ai().find('table tr input').length;
    var _FmtStatus=document.getElementById('contentIframe').contentWindow.$("body").data("_FmtStatus");
    _FmtStatus.isAdd = false;
    _FmtStatus.isCompile = -1;
    _FmtStatus.isMoved = false;
    testConfig.testFun.ai().find(".addTeamClassBtn")[0].click();
  };
  this.step3 = function () {
    var afterTr = testConfig.testFun.ai().find('table tr input').length;
    testConfig.testFun.ai().find('table tr:last-child input').val(JSON.parse(caseData.preDataDetail).data.categoryName);
    var ve=testConfig.testFun.ai().find("#class-con")[0].__vue__;
    ve.rows[ve.rows.length-1].projectType = JSON.parse(caseData.preDataDetail).data.categoryName;
    msg.stepResult = {
      byname:"addRow",
      realData:afterTr>this.beforeTr
    };
    ipcRenderer.sendToHost(JSON.stringify(msg));
    msg.stepResult = {
      byname:"teamCategory",
      realData:testConfig.testFun.ai().find('table tr:last-child input').val()
    };
    ipcRenderer.sendToHost(JSON.stringify(msg));
  };
  this.step4 = function () {
    var r = parseInt(window.sessionStorage.getItem("runTimes"));
    console.log();
    if(!planData.cases[r+1]){
      msg.isOver = 'true';
    }
    testConfig.testFun.ai().find(".sort-confirm")[0].click();
    msg.stepResult = {
      byname:"hold",
      realData:testConfig.testFun.ai().find('table tr:last-child input').val()
    };
    ipcRenderer.sendToHost(JSON.stringify(msg));
  };
  this.step5 = function(){
    var address = window.location.href;
    var r = parseInt(window.sessionStorage.getItem("runTimes"));
    if(planData.cases[r+1].caseNumber){
      if (address.indexOf("?") > 0) {
        window.location.href = address + "&_from=" + planData.cases[r+1].caseNumber;
      } else {
        window.location.href = address + "?_from=" + planData.cases[r+1].caseNumber;
      }
    }
    window.sessionStorage.setItem("runTimes",r+1);
  }
}
function testStart() {
  var test = new SearchProject();
  test.init();
}
exports.start = testStart;